package com.tool_stdy.utils.guava;

import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

public class GuavaCatchCallback {

    private static Cache<String, String> loadc = null;
    static {
        loadc = CacheBuilder.newBuilder().expireAfterAccess(5, TimeUnit.SECONDS).build();
    }

    public static String getDataFromDB(String name) {
        System.out.println(name + " get  from db doing...");
        return "hello " + name;
    }

    public static void main(String[] args) throws Exception {
        String result1 = loadc.get("lisi", new Callable<String>() {
            @Override
            public String call() throws Exception {
                return getDataFromDB("lisi");
            }
        });
        System.out.println("第一次执行 " + result1);//第一次访问catch中没有 执行Callable方法
        Thread.sleep(3000);


        String result2 = loadc.get("lisi", new Callable<String>() {
            @Override
            public String call() throws Exception {
                return getDataFromDB("lisi");
            }
        });
        System.out.println("第二次执行:" + result2);
    }
}
